package se.webbzon.oschi01.worldcamera;

import java.awt.Dimension;
import java.awt.image.ImageObserver;

import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Node;
import javax.vecmath.Point3d;

import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldarea.WorldAppearance3D;

public class JWorldCameraBackground extends WorldCameraPane implements ImageComponent2D.Updater {
	
	// The background image component 
	private final ImageComponent2D image;
	
	// The java3d background of this camera background
	private final Background background;
	
	// The current world appearance for rendering the background
	private WorldAppearance3D world;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new world camera background.  **/
	@Friendly JWorldCameraBackground(ImageObserver observer, Dimension d) {
		super(observer,d);
		image = new ImageComponent2D(	ImageComponent2D.FORMAT_RGBA,
										getLayer().getBufferedImage(),true,false);
		image.setCapability(ImageComponent2D.ALLOW_IMAGE_READ);
		image.setCapability(ImageComponent2D.ALLOW_IMAGE_WRITE);
		background = new Background(image);
		background.setImageScaleMode(Background.SCALE_FIT_ALL);
		background.setApplicationBounds(new BoundingSphere(new Point3d(),1000));
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the java3d node associated with this instance. This method
	 * should only be used by by the WorldViewFactory. **/
	public final Node getNode() {
		return background;
	}
	
	/** Repaints the background world camera background in the content
	 * provided by the given WorldAppearance3D. **/
	public void repaint(WorldAppearance3D world) {
		Dimension d = new Dimension();
		getLayer().getDimension(d);
		this.world = world;
		image.updateData(this, 0, 0, d.width, d.height);
	}
	
	@Override public void updateData(	ImageComponent2D image, 
										int x, int y, int width, int height) {
		clear();
		world.performPreDraw(this);
	}

}
